<?php
/**
 * 作者：云喵软件开发工作室
 * 官网：https://www.meoyun.com
 */

namespace app\model;

class UserFollowModel extends BaseModel
{
    protected $table = 'user_follow';

    /**
     * 检查用户是否已关注某个用户
     */
    public static function isFollowing($userId, $followUserId)
    {
        return self::where('user_id', $userId)
            ->where('follow_user_id', $followUserId)
            ->exists();
    }

    /**
     * 关注用户
     */
    public static function followUser($userId, $followUserId)
    {
        // 检查是否已经关注
        if (self::isFollowing($userId, $followUserId)) {
            return false;
        }

        // 不能关注自己
        if ($userId === $followUserId) {
            return false;
        }

        return self::create([
            'user_id' => $userId,
            'follow_user_id' => $followUserId
        ]);
    }

    /**
     * 取消关注用户
     */
    public static function unfollowUser($userId, $followUserId)
    {
        return self::where('user_id', $userId)
            ->where('follow_user_id', $followUserId)
            ->delete();
    }

    /**
     * 检查两个用户是否互相关注
     */
    public static function isMutualFollowing($userId, $followUserId)
    {
        // 检查用户A是否关注用户B
        $userFollowsTarget = self::isFollowing($userId, $followUserId);
        
        // 检查用户B是否关注用户A
        $targetFollowsUser = self::isFollowing($followUserId, $userId);
        
        return $userFollowsTarget && $targetFollowsUser;
    }
}